package com.seantone.inappupdate.vivo;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class VivoUtils {
    /**
     * 获取加密验签
     */
    public static void getSign() {
        String secret = "xxxxxxxxxxxxxxxxxxxxxxxxx";

        Map<String,Object> parmas = new HashMap();
        parmas.put("account", "test");
        parmas.put("timestamp", System.currentTimeMillis());
        parmas.put("bizParam", getBizParam());
        String sign =HmacSHA256(getUrlParamsFromMap(parmas), secret);
        System.out.println("得到加密后的验签：" + sign);
    }
    public static Map<String,String> getBizParam() {
        Map<String,String> bizParams = new HashMap();
        bizParams.put("packageName", "com.xxx.xxx");
        bizParams.put("androidVersion", "4.0.1");
        bizParams.put("model", "vivo x20");
        return bizParams;
    }
    /**
     * 根据传入的map，把map里的key value转换为接口的请求参数，并给参数按ascii码排序
     *
     * @param paramsMap 传入的map
     * @return 按ascii码排序的参数键值对拼接结果
     */
    public static String getUrlParamsFromMap(Map<String, Object> paramsMap) {
        List<String> keysList = new ArrayList<>(paramsMap.keySet());
        Collections.sort(keysList);
        StringBuilder sb = new StringBuilder();
        for (String key : keysList) {
            if ("sign".equals(key)) {
                continue;
            }
            Object object = paramsMap.get(key);
            if (object == null) {
                continue;
            }
            sb.append(key).append("=");
            if ("bizParam".equals(key)) {
               // sb.append(JSON.toJSONString(object)).append("&");
            } else {
                sb.append(object).append("&");
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
    /**
     * HMAC_SHA256 验签加密
     * */
    public static String HmacSHA256(String data, String key) {
        try {
            byte[] secretByte = key.getBytes(Charset.forName("UTF-8"));
            SecretKeySpec signingKey = new SecretKeySpec(secretByte, "HmacSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(signingKey);
            byte[] dataByte = data.getBytes(Charset.forName("UTF-8"));
            byte[] by = mac.doFinal(dataByte);
            return byteArr2HexStr(by);
        } catch (Exception e) {
          e.printStackTrace();
        }
        return null;
    }
    private static String byteArr2HexStr(byte[] arrB) {
        int iLen = arrB.length;
        // 每个byte用两个字符才能表示，所以字符串的长度是数组长度的两倍
        StringBuilder sb = new StringBuilder(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            // 把负数转换为正数
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            // 小于0F的数需要在前面补0
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    public static void jumpToAppStoreDetailUpdate(Context context) {
        Intent intent = new Intent();
        String packageName = context.getPackageName();
        String url = "vivomarket://details?id=" + packageName +"&th_name=self_update";
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        intent.setPackage("com.bbk.appstore");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }
}
